package com.umeng.soexample.mvp.persenter;

import com.umeng.lib_core.mvp.presenter.BasePresenter;
import com.umeng.soexample.bean.AddressListEntity;
import com.umeng.soexample.bean.CarListEntity;
import com.umeng.soexample.bean.CouponListEntity;
import com.umeng.soexample.mvp.contract.AffirmOrderFragmentContract;

import org.json.JSONException;
import org.json.JSONObject;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;


public class AffirmOrderFragmentPersenter extends BasePresenter<AffirmOrderFragmentContract.AffirmOrderFragmentIView, AffirmOrderFragmentContract.AffirmOrderFragmentIModel> {
    @Inject
    public AffirmOrderFragmentPersenter(AffirmOrderFragmentContract.AffirmOrderFragmentIView affirmOrderFragmentIView, AffirmOrderFragmentContract.AffirmOrderFragmentIModel affirmOrderFragmentIModel) {
        super(affirmOrderFragmentIView, affirmOrderFragmentIModel);
    }

    /**
     * 获取地址列表
     * @param user_id
     */
    public void addressListRequest(String user_id){
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("user_id", user_id);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        m.address_list(jsonObject.toString())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<AddressListEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        v.showDialog();
                        disposable.add(d);
                    }

                    @Override
                    public void onNext(AddressListEntity entity) {
                        v.hideDialog();
                        v.addressListSuccess(entity);
                    }

                    @Override
                    public void onError(Throwable e) {
                        v.hideDialog();
                        v.showMessage(e.toString());
                    }

                    @Override
                    public void onComplete() {
                        v.hideDialog();
                    }
                });
    }

    /**
     * 获取优惠券列表
     */
    public void couponListRequest(String user_id){
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("user_id", user_id);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        m.coupon_list(jsonObject.toString())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<CouponListEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        v.showDialog();
                        disposable.add(d);
                    }

                    @Override
                    public void onNext(CouponListEntity entity) {
                        v.hideDialog();
                        v.couponListSuccess(entity);
                    }

                    @Override
                    public void onError(Throwable e) {
                        v.hideDialog();
                        v.showMessage(e.toString());
                    }

                    @Override
                    public void onComplete() {
                        v.hideDialog();
                    }
                });
    }

    /**
     * 获取购物车列表
     */
    public void carListRequest(String user_id){
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("userid", user_id);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        m.car_list(jsonObject.toString())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<CarListEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        v.showDialog();
                        disposable.add(d);
                    }

                    @Override
                    public void onNext(CarListEntity entity) {
                        v.hideDialog();
                        v.carListSuccess(entity);
                    }

                    @Override
                    public void onError(Throwable e) {
                        v.hideDialog();
                        v.showMessage(e.toString());
                    }

                    @Override
                    public void onComplete() {
                        v.hideDialog();
                    }
                });
    }
}
